package com.xiong.excel;


import org.apache.poi.ss.usermodel.*;

import java.io.File;

public class ExcelReader {

    public static void main(String[] args) {
        try {
            Workbook workbook = WorkbookFactory.create(new File("C:\\Users\\86186\\Desktop\\新建文件夹\\新建 Microsoft Excel 工作表.xlsx")); // 加载Excel文件
            Sheet sheet = workbook.getSheetAt(0); // 获取第一个sheet

            int rowCount = sheet.getLastRowNum() + 1; // 获取行数，从0开始计数，所以需要加1

            for (int i = 0; i < rowCount; i++) {
                Row row = sheet.getRow(i);
                int cellCount = row.getLastCellNum(); // 获取行中的单元格数，从0开始计数

                for (int j = 0; j < cellCount; j++) {
                    Cell cell = row.getCell(j);
                    // 获取单元格的值
                    String cellValue = "";
                    if (cell != null) {
                        switch (cell.getCellType()) {
                            case STRING:
                                cellValue = cell.getStringCellValue();
                                break;
                            case NUMERIC:
                                cellValue = String.valueOf(cell.getNumericCellValue());
                                break;
                            case BOOLEAN:
                                cellValue = String.valueOf(cell.getBooleanCellValue());
                                break;
                            case FORMULA:
                                cellValue = cell.getCellFormula();
                                break;
                            default:
                                cellValue = String.valueOf(cell.getStringCellValue());
                        }
                    }

                    System.out.println("Cell value: " + cellValue);
                }
            }

            workbook.close(); // 关闭工作簿
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
